﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component.DataFactory;
using SyCODE.Component;
using System.Xml;
using System.IO;
using System.Web.Security;

namespace SyCODE.Blog
{
    /// <summary>
    /// 删除一条日志引用记录
    /// </summary>
    public class DeleteTrackBack : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            NameValueCollection nvc = new NameValueCollection(context.Request.Form);

			Ticket ticket = Ticket.Get(FormsAuthentication.FormsCookieName);

            if (Ticket.Authenticate(ticket))
            {
                string path = Path.Combine(AppPath.GetUserPhysicalPath(ticket.Name), @"\topic\" + nvc["id"] + @"\trackback.xml");
                XmlDocument tbdom = new XmlDocument();
                tbdom.Load(path);

                int index = Int32.Parse(nvc["idx"]);

                XmlNode body = tbdom.DocumentElement.SelectSingleNode("body");
                if (body.ChildNodes.Count > index)
                {
                    //将引用节点移除
                    body.RemoveChild(body.ChildNodes[index]);
                    tbdom.Save(path);

                    nvc.Add("u", ticket.Name);

                    DBAM dbam = new DBAM();
                    dbam.BuildParameters("deletetrackback", nvc);

                    try
                    {
                        dbam.Execute();
                    }
                    finally
                    {
                        dbam.Dispose();
                    }
                }
            }
            else
            {
                Function.ClientAlert(StringResource.GetField("authfaild"));
            }
        }

        #endregion
    }
}